package com.kdtech.analyse.blog;
import com.alibaba.fastjson.JSONObject;
import com.kdtech.crawler.CrawlHTML;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;



import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.analyse.AnalyseNews;

/**
 * http://www.bokee.com/ 博客网
 * @author 小聂
 */
public class BokeeBlogAnalyse implements AnalyseNews{
	
	public boolean isDetailPage(String url) {
		boolean bRet = false;
		//http://2459717288.bokee.com/501145852.html
		String[] regex = {
				"http://.*[.]bokee.com/[0-9]+.html",
				"http://.*[.]bokee.com/viewdiary.[0-9]+.html",
				"http://wei.bokee.com/blog/viewDiary.action[?]diaryId=[0-9]*&uid=[0-9]*"
				};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		String url = urlMeta.getUrl();

		String html = urlMeta.getHtml();

		if (html== null) {
		}
		NewsMeta blog = new NewsMeta();
		blog.setUrl(url);
		String title = null;
		String content = null;
		Long date = null;
		Integer clickNum=0;
		Integer commentNum=0;
		Integer deliverNum=0;
		String dateStr = null;
		String textComment = null;
		String author = null;
		Document doc = Jsoup.parse(html);

			title = doc.select("span[itemprop=articleSection]").text();
			if(StringUtils.isBlank(title)){
				title  = doc.select("html body#entry-view div#container div#pageBody div#centerDiv div#blogIndex.operation div.col-body div.newly-right div.entry-title div.lt h2").text();
			}
			dateStr = doc.select("html body#entry-view div#container div#pageBody div#centerDiv div#blogIndex.operation div.col-body div.newly-right div.fbzt span.f span.pub-date").text();
			dateStr += doc.select("html body#entry-view div#container div#pageBody div#centerDiv div#blogIndex.operation div.col-body div.newly-right div.fbzt span.f span.pub-time").text();
			date = DateUtils.matchDate(dateStr);
			if(date==null){
				date = DateUtils.matchDate(doc.select("div.tag").text());
			}
			author = doc.select("div.content div.top div.tag span a span span[itemprop=name]").text();
			content = HtmlCleaner.getContentHtml(url, doc.select("div#blogIndex.operation div.col-body div.newly-right div.entry-body"));
			if(StringUtils.isBlank(content)){
				content = HtmlCleaner.getContentHtml(url,  doc.select("div.txt[itemprop=articleBody]"));
			}
			String bid = doc.select("input[name=fid]").attr("value");
			String updateUrl = "http://home.bokee.com/home.php/numbers/showItemNum/uname/callback/?bid="+bid;
				try{
					UrlMeta responseToURL = CrawlHTML.responseToURL(updateUrl);
					if(responseToURL!=null){
						String html2 = responseToURL.getHtml().trim();
						if(html2.startsWith("(")){
							html2 = html2.substring(1);
							if(html2.endsWith(")")){
								html2= html2.substring(0,html2.length()-1);
							}
						}
						JSONObject fromObject = JSONObject.parseObject(html2);
						clickNum = fromObject.getIntValue("click");
					commentNum = fromObject.getIntValue("comment");
					}
				}catch(Exception er){}


		blog.setCommentNum(commentNum);
		blog.setClickNum(clickNum);
		//blog.setDeliverNum(deliverNum);
		blog.setUpdateUrl(updateUrl);
		blog.setTitle(title);
		blog.setAuthor(author);
		blog.setContent(content);
		blog.setDate(date);
		return blog;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		if(meta!=null){
			String updateUrl = meta.getUpdateUrl();
			if(updateUrl!=null){
				UrlMeta responseToURL = CrawlHTML.responseToURL(updateUrl);
				if(responseToURL!=null){
					String html2 = responseToURL.getHtml().trim();
					if(html2.startsWith("(")){
						html2 = html2.substring(1);
						if(html2.endsWith(")")){
							html2= html2.substring(0,html2.length()-1);
						}
					}
					JSONObject fromObject = JSONObject.parseObject(html2);
					meta.setClickNum(fromObject.getIntValue("click"));
					meta.setCommentNum(fromObject.getIntValue("comment"));
				}
			}
		}
		return null;
	}





	public static void main(String[] args) {
		String url = "http://wd989898.bokee.com/501215767.html";
		BokeeBlogAnalyse test = new BokeeBlogAnalyse();
		if(test.isDetailPage(url)){
			UrlMeta responseToURL = CrawlHTML.responseToURL(url);
			NewsMeta parserHtml = test.parserHtml(responseToURL);
			System.out.println(parserHtml);
		}else{
			System.out.println("不符合规则");
		}
	}

	
}
